<?php
namespace Admin\Controller;
use Admin\Controller\CheckController;
use Admin\Controller\CarController;
use Admin\Controller\ComplainController;
class BankController extends AdminController{
	//银行
    protected function _initialize()
    {
		parent::_initialize ();
	}

	//添加银行
	public function add()
	{
	  if(IS_POST)
	  {
	  	$data = array(
			'bank_name'       => $_POST['bankName'],
			'bank_short_name' => $_POST['bankShortName'],
			'create_time'     => time(),
			'update_time'     => time(),
			'status'          => $_POST['status'],
			'pic'             => $_POST['pic'],
	  		);
	  	$result = $this->updateData($data, 'bank');
		$this->returnResult($result);
	  }
	  $this->display();
	}

	public function edit()
	{
		if(IS_POST)
		{
          	$data = array(
				'bank_name'       => $_POST['bankName'],
				'bank_short_name' => $_POST['bankShortName'],
				'update_time'     => time(),
				'status'          => $_POST['status'],
				'pic'             => $_POST['pic'],
	  		);
	  		$result = M('bank')->where('id='.I('post.id'))->save($data);
	  		$this->returnResult($result);
		}else{
			$info = $this->getInfo('*','bank','id='.I('get.id',0,'intval'));
			$this->assign('info',$info);
			$this->display('add');
		}

	}

	public function index()
	{
		$where='1=1';
		$where .= I('get.bank_name') ? ' and bank_name like "%' . I('get.bank_name') . '%"' : '';
		$where .= I('get.status', -1) > -1 ? ' and status =' . I('get.status') : '';
		$list=$this->getList('*','bank',$where,'id desc',true);
		$this->assign('list',$list);
		$this->display();
	}

	public function del()
	{
		//$result = M('bank')->where('id=' . I('get.id', 0, 'intval'))->setField('status', 2);
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'bank');
		$this->returnResult($result);
	}

	public function add_info()
	{
		$this->display();
	}

	//申请返佣
	public function Rebate()
	{
		$this->display();
	}

	//银行投诉列表
	public function Complain_index()
	{
		$status=$_GET['status']!='' ? $_GET['status'] : '';
		$com=new ComplainController();
		$list=$com->get_complain($status,2);
		$this->assign('list',$list);
		$this->display();
	}
	public function complain_add(){
		if(IS_POST){
			   $group=$this->getInfo('*','group','id='.$_POST['gid']);
	           $data= array(
	           	'group_id' =>$_POST['gid'] ,
	           	'group_name'=>$group['name'],
	           	'title' =>$_POST['title'] ,
	           	'type' =>$_POST['complain_type'] ,
	           	'user_name' =>$_POST['user_name'] ,
	           	'user_phone' =>$_POST['user_phone'] ,
	           	'desc' =>$_POST['desc'] ,
	           	'add_time' =>time() ,
	           	'business_type'=>2,
	           	'update_time'=>time()
	           	);
	           $result=M('complain')->add($data);
	           if($result){
	             $this->success('录入成功',U('Bank/Complain_index'));
	           }else{
	           	 $this->error('录入失败',U('Bank/Complain_index'),0);
	           	}
           }else{
				$group=$this->getList('*','group','work_status=1 and pid=0 and status=1');
				$this->assign('group',$group);
				$this->display();
		}
	}
	//银行投诉处理
	public function Complain_deal()
	{
		$status=$_GET['status']!='' ? $_GET['status'] : '';
		$com=new ComplainController();
		$list=$com->get_complain($status,2);
		$deal=1;
		$this->assign('deal',$deal);
		$this->assign('list',$list);
		$this->display('Complain_index');
	}

	public function Complain_dealInfo(){
	    if(IS_POST){
            $data=array(
              'status'=>1,
              'del_result'=>$_POST['del_result'],
              'update_time'=>time(),
            	);
            $result=M('complain')->where('id='.$_POST['id'])->save($data);
            if($result){
              $this->success('处理成功',U('Bank/Complain_deal'));
            }else{
              $this->error('处理失败',U('Bank/Complain_deal'),0);
            }
		}else{
			$complain=$this->getInfo('*','complain','id='.I('get.id',0,'intval'));
			$this->assign('complain',$complain);
			$this->display();
		}
	}
	//银行投诉删除
	public function Complain_del()
	{
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'complain');
		if($result){
          $this->success('删除成功',U('Complain/index'));
		}else{
			$this->error('删除失败',U('Complain/index'));
		}
	}

	//银行还款卡接收
	public function Repay_receive(){
	    $info=new CheckController();
	    $list=$info->getCheck_status(19,'=1');
	    $this->assign('list',$list);
	    $this->display();
	}
   //还款卡导入
   	public function Repay_import(){
		 if(IS_POST){
            set_time_limit(0);
	        ignore_user_abort(true);//检测用户断开
			if (!empty ( $_FILES ['photo'] ['name'] )){
			    $tmp_file = $_FILES ['photo'] ['tmp_name'];
			    $file_types = explode ( ".", $_FILES ['photo'] ['name'] );
			    $file_type = $file_types [count ( $file_types ) - 1];
			     /*判别是不是.xls文件，判别是不是excel文件*/
			     if ($file_type !== "xls"){
			          $this->error('不是Excel文件，重新上传');
		    }
	     $savePath = './Uploads/excel/';
	     $str = date ( 'Ymdhis' );
	     $file_name = $str . "." . $file_type;
	     if (!move_uploaded_file($_FILES['photo']['tmp_name'],$savePath.$file_name)){
	        $this->error('上传失败！');
	      }
	      $read=new CarController();
	      $res = $read->read($savePath . $file_name);
	      unset($res[1]);
		}else{
			$this->error('附件不能为空！');
		}
		$tems  = 0;
		foreach ($res as $k => $v) {
			$user=$this->getInfo('*','user','identity_no='."'".$v[0]."'");
			if(!$user){
             $this->error('该客户不存在');
			}
			$data['identity_no']=$v[0];
			$data['bank_card']=$v[1];
			$data['bank_name']=$v[2];
			$data['user_id']=$user['id'];
			$data['user_name']=$user['user_name'];
			$data['add_time']=time();
			$data['update_time']=time();
			$result=M('bank_repay')->add($data);
			if(!$result){
               $this->error('添加失败');
			}
		    $tems++;
		}
		    $this->success("批量执行成功了：".$tems."条",U('Bank/Repay_index'));
	    }else{
	    	$this->display();
	    }

	}

	//还款卡管理（银行驻点）
	public function Repay_index(){
		$list=$this->Bank_Repay(0);
		$type=1;
		$this->assign('type',$type);
		$this->assign('list',$list);
		$this->display();
	}
	//客户还款卡管理（渠道内勤）
     public function Repay_user(){
     	$where='1=1';
        $where .= I('get.user_name') != '' ? ' and user_name like "%'.I('get.user_name').'%"' : '';
        $where .= I('get.create_time_beg') != '' ? ' and add_time >= '.strtotime(I('get.create_time_beg')) : '';

        $where .= I('get.create_time_end') != '' ? ' and add_time < '.strtotime(I('get.create_time_end')) : '';
        $where .= ' and (status=2 or status=3)';
        $list=$this->getList('*','bank_repay',$where,'id desc',true);
		$this->assign('list',$list);
		$this->display('Repay_index');
     }


	public function set_status(){
		$id=I('get.id',0,'intval');
		$repayInfo=$this->getInfo('*','bank_repay','id='.$id);

		$status=I('get.status',0,'intval');
		$repay=array('update_time'=>time(),'status'=>$status);
		$result=M('bank_repay')->where('id='.$id)->save($repay);
		if($result){
          $data=$this->getStatus($status);
          $log=array('user_id'=>$repayInfo['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>$data['note'],'note'=>$data['note'],'add_time'=>time(),'check_status'=>$data['check_status']);
          M('check_log')->add($log);
          $this->success('操作成功');
		}else{
			$this->error('操作失败');
		}

	}

	public function getStatus($status){
		switch ($status) {
			case '1':
				$data['note']='还款卡已寄送';
				$data['check_status']=40;
				break;
			case '3':
				$data['note']='还款卡快递确认';
				$data['check_status']=42;
				break;
			default:
				# code...
				break;
		}

		return $data;
		exit;
	}


	public function Bank_Repay($status=0){
		$where='1=1';
        $where .= I('get.user_name') != '' ? ' and user_name like "%'.I('get.user_name').'%"' : '';
       // $where .= I('get.telephone') != '' ? ' and user_phone = '.I('get.telephone') : '';
        $where .= I('get.create_time_beg') != '' ? ' and add_time >= '.strtotime(I('get.create_time_beg')) : '';

        $where .= I('get.create_time_end') != '' ? ' and add_time < '.strtotime(I('get.create_time_end')) : '';
        $where .= ' and status='.$status;
        $list=$this->getList('*','bank_repay',$where,'id desc',true);
        return $list;
	}
//还款卡客户确认
	public function confirm_img(){
        $repayInfo=$this->getInfo('*','bank_repay','id='.$_POST['id']);
		$data=array(
           'confirm_img'=>$_POST['img'],
           'update_time'=>time(),
           'status'=>4
			);
		$result=M('bank_repay')->where('id='.$_POST['id'])->save($data);

		if($result){
           $log=array('user_id'=>$repayInfo['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>'(还款卡)照片已确认','note'=>'(还款卡)照片已确认','add_time'=>time(),'check_status'=>43);
           M('check_log')->add($log);
           redirect('/Admin/Bank/Repay_user');
		}else{
			$this->error('操作失败',U('Bank/Repay_user'),0);
		}
	}

}